package com.bnz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bnz.mapper.ClassesMapper;
import com.bnz.pojo.Classes;
import com.bnz.pojo.PageResult;
import com.bnz.pojo.Student;
import com.bnz.mapper.StudentMapper;
import com.bnz.pojo.StudentVo;
import com.bnz.service.IStudentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author mch
 * @since 2022-10-19
 */
@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements IStudentService {

    @Autowired
    private ClassesMapper classesMapper;

    //1. 条件查询
    @Override
    public List<Student> findByWords(StudentVo vo) {
        //1.1 开始封装查询条件
        QueryWrapper<Student> queryWrapper = new QueryWrapper<>();
        if (vo != null){
            if (StringUtils.isNotBlank(vo.getSname())){
                queryWrapper.like("sname",vo.getSname());
            }
            if (StringUtils.isNotBlank(vo.getAddr())){
                queryWrapper.like("addr",vo.getAddr());
            }
        }
        List<Student> students = baseMapper.selectList(queryWrapper);
        //1.2 将学生与班级名称绑定关系
        for (Student student : students) {
            Classes classes = classesMapper.selectById(student.getCid());
            student.setSname(classes.getCname());
        }
        //1.3 返回
        return students;
    }

    //2. 分页查询
    @Override
    public PageResult<Student> findByPage(int page, int pageSize) {
        //2.1 封装分页查询参数
        IPage<Student> ipage =  new Page<>(page,pageSize);
        //2.2 开始分页查询
        IPage<Student> studentIPage = baseMapper.selectPage(ipage, null);
        //2.3 返回
        return new PageResult<>(studentIPage.getTotal(),studentIPage.getRecords());
    }

    //3. 分页查询带条件
    @Override
    public PageResult<Student> search(int page, int pageSize, StudentVo vo) {
        //3.1 封装分页参数
        Page<Student> ipage = new Page<>(page,pageSize);
        //3.2 封装查询条件
        QueryWrapper<Student> wrapper = new QueryWrapper<>();
        if (vo != null){
            if (StringUtils.isNotBlank(vo.getSname())){
                wrapper.like("sname",vo.getSname());
            }
            if (StringUtils.isNotBlank(vo.getAddr())){
                wrapper.like("addr",vo.getAddr());
            }
            if (vo.getCid() != null && vo.getCid() != 0){
                wrapper.eq("cid",vo.getCid());
            }
        }
        //3.3 开始分页查询
        Page<Student> studentPage = baseMapper.selectPage(ipage, wrapper);
        //3.4 返回
        return new PageResult<>(studentPage.getTotal(),studentPage.getRecords());
    }

    //4. 删除学生
    @Override
    public void delete(String sid) {
        baseMapper.deleteById(sid);
    }
}
